local haoshi = fk.CreateSkill {

  name = "joyex__haoshi",

  tags = { Skill.Compulsory, },

}



haoshi:addEffect(fk.DrawNCards, {
  name = "joyex__haoshi",
  anim_type = "drawcard",
  events = {fk.DrawNCards},
  can_trigger = function (self, event, target, player, data)
    return target == player and player:hasSkill(haoshi.name)
  end,
  on_cost = function (self, event, target, player, data)
    return true
  end,
  on_use = function(self, event, target, player, data)
    data.n = data.n + 2
  end,
})
haoshi:addEffect(fk.TurnStart, {
  refresh_events = {fk.TurnStart,fk.TargetConfirmed},
  can_refresh = function (self, event, target, player, data)
    if target ~= player then return false end
    local yes = false
    for _, p in ipairs(player.room:getOtherPlayers(player)) do
      if type(p:getMark("@@joyex__haoshi")) == "table" and table.contains(p:getMark("@@joyex__haoshi"),player.id)then
        yes = true
        break
      end
    end
    if event == fk.TurnStart then
      return yes
    elseif event == fk.TargetConfirmed then
      return yes and data.card and (data.card.trueName == "slash" or data.card:isCommonTrick())
    end
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    if event == fk.TurnStart then
      for _, p in ipairs(player.room:getOtherPlayers(player)) do
        local mark = type(p:getMark("@@joyex__haoshi")) == "table" and p:getMark("@@joyex__haoshi") or {}
        table.removeOne(mark,player.id)
        if #mark == 0 then
          room:setPlayerMark(p,"@@joyex__haoshi",0)
        else
          room:setPlayerMark(p,"@@joyex__haoshi",mark)
        end
      end
    elseif event == fk.TargetConfirmed then
      for _, p in ipairs(player.room:getOtherPlayers(player)) do
        if type(p:getMark("@@joyex__haoshi")) == "table" and table.contains(p:getMark("@@joyex__haoshi"),player.id) and not p:isKongcheng() then
          local card = room:askForCard(p,1,1,false,haoshi.name,true,".","你可以交给 "..Fk:translate(player.id).." 一张手牌")
          if #card > 0 then
            room:moveCardTo(card,Card.PlayerHand,player,fk.ReasonGive,haoshi.name,nil,false,player)
          end
        end
      end
    end
  end,
})
haoshi:addEffect(fk.TargetConfirmed, {
  refresh_events = {fk.TurnStart,fk.TargetConfirmed},
  can_refresh = function (self, event, target, player, data)
    if target ~= player then return false end
    local yes = false
    for _, p in ipairs(player.room:getOtherPlayers(player)) do
      if type(p:getMark("@@joyex__haoshi")) == "table" and table.contains(p:getMark("@@joyex__haoshi"),player.id)then
        yes = true
        break
      end
    end
    if event == fk.TurnStart then
      return yes
    elseif event == fk.TargetConfirmed then
      return yes and data.card and (data.card.trueName == "slash" or data.card:isCommonTrick())
    end
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    if event == fk.TurnStart then
      for _, p in ipairs(player.room:getOtherPlayers(player)) do
        local mark = type(p:getMark("@@joyex__haoshi")) == "table" and p:getMark("@@joyex__haoshi") or {}
        table.removeOne(mark,player.id)
        if #mark == 0 then
          room:setPlayerMark(p,"@@joyex__haoshi",0)
        else
          room:setPlayerMark(p,"@@joyex__haoshi",mark)
        end
      end
    elseif event == fk.TargetConfirmed then
      for _, p in ipairs(player.room:getOtherPlayers(player)) do
        if type(p:getMark("@@joyex__haoshi")) == "table" and table.contains(p:getMark("@@joyex__haoshi"),player.id) and not p:isKongcheng() then
          local card = room:askForCard(p,1,1,false,haoshi.name,true,".","你可以交给 "..Fk:translate(player.id).." 一张手牌")
          if #card > 0 then
            room:moveCardTo(card,Card.PlayerHand,player,fk.ReasonGive,haoshi.name,nil,false,player)
          end
        end
      end
    end
  end,
})

haoshi:addEffect("active", {
  name = "#joyex__haoshi_active",
  visible = false,
  can_use = Util.FalseFunc,
  target_num = 1,
  target_filter = function (self, to_select, selected, selected_cards, card, extra_data)
    return to_select ~= player.id
  end,
  card_num = function ()
    return player:getHandcardNum() // 2
  end,
  card_filter = function(self, player, to_select, selected)
    return #selected < player:getHandcardNum() // 2 and Fk:currentRoom():getCardArea(to_select) == Player.Hand
  end,
})

haoshi:addEffect(fk.AfterDrawNCards, {
  name = "#joyex__haoshi_give",
  events = {fk.AfterDrawNCards},
  mute = true,
  anim_type = "support",
  frequency = Skill.Compulsory,
  visible = false,
  can_trigger = function(self, event, target, player, data)
    return target == player and player:usedSkillTimes("joyex__haoshi", Player.HistoryPhase) > 0 and player:getHandcardNum() > 5
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local cards = {}
    local target
    local _, ret = room:askForUseActiveSkill(player, "#joyex__haoshi_active", "#joyex__haoshi-givecard:::"..player:getHandcardNum() // 2,true)
    if not (_ and ret) then return end
    if ret then
      cards = ret.cards
      target = room:getPlayerById(ret.targets[1])
    else
      cards = table.random(player:getCardIds(Player.Hand), player:getHandcardNum() // 2)
    end
    room:moveCardTo(cards,Card.PlayerHand,target,fk.ReasonGive,haoshi.name,nil,false,player)
    if player.dead or target.dead then return false end
    local mark = type(target:getMark("@@joyex__haoshi")) == "table" and target:getMark("@@joyex__haoshi") or {}
    table.insertIfNeed(mark,player.id)
    room:setPlayerMark(target,"@@joyex__haoshi",mark)
  end
})

return haoshi